<?php 
namespace xfyzf\api\controller;
use xfyzf\api\controller\Examine;
class Cron{
    public function aliqqyun(){
        global $conf;
        Examine::cronkey(input("get")['key']);
        $timess = time() + mt_rand(5, 10);
        $cron = ($conf['aliqqpay_cron'] == 1) ? "AND hook_type IN ('1','0')" : "AND hook_type = '0'";
        $sql = "SELECT * FROM pay_qrlist 
            WHERE status='1' 
            AND type IN ('alipay', 'qqpay') 
            {$cron} 
            AND crontime < '{$timess}' 
            AND cookie NOT IN ('xfqymckxy', 'xfqydmfxy') 
            LIMIT ".cronlimit; 
        $rs = safeSqlQuery($sql, []);
        if ($rs['count'] > 0) {
            foreach ($rs['result'] as $res) {
                echo check_money_notify($res, true);
            }
        }
        $time = time() - 120;
        // 合并查询，减少数据库访问次数
        $sql = "SELECT * FROM pay_qrlist 
                WHERE status='1' 
                AND hook_type='1' 
                AND crontime < {$time} LIMIT ".cronlimit; 
        $rs = safeSqlQuery($sql, []);
        if ($rs['count'] > 0) {
            foreach ($rs['result'] as $res1) {
                xfpcgua($res1);
            }
        }
    
        // 优化查询2：避免使用 ORDER BY RAND()
        $sql = "SELECT * FROM pay_wechat_trumpet 
                WHERE status='1' 
                AND login_time < '{$timess}' 
               LIMIT ".cronlimit;   
        $rs = safeSqlQuery($sql, []);
        if ($rs['count'] > 0) {
            foreach ($rs['result'] as $res2) {
                if ($res2['login_time'] < time() + mt_rand(3, 5)) {
                    wxpcdycron($res2);
                }
            }
        }
    
        echo 'Cron Ok';
    }
    public function wxyun(){
        Examine::cronkey(input("get")['key']);
        $time=time();//当前时间戳
        $sql = "SELECT * FROM pay_qrlist WHERE status='1' AND type='wxpay' AND hook_type='2' AND crontime<'{$time}' LIMIT ".cronlimit; 
        $rs = safeSqlQuery($sql, []);
        if($rs['count']>0){
            foreach ($rs['result'] as $res){
                $data = wxyun_money_notify($res);
                echo $data;
            }
        }
        echo 'Cron Ok '; 
    }
    
    public function fkjk(){
        Examine::cronkey(input("get")['key']);
        $sql = "SELECT * FROM pay_qrlist WHERE stat='1' limit 1";
        $row = executeSQLQuery($sql);
        $types = [
        'qqpay' => 'QQ钱包',
        'alipay' => '支付宝',
        'wxpay' => '微信'
         ];
       $gf= $types[$row['type']];
       $sql  = "SELECT email FROM pay_user WHERE pid='{$row['pid']}' limit 1";
       $userrow = executeSQLQuery($sql);
       $sql = "SELECT count(*) as count FROM pay_order WHERE qr_id='{$row['id']}' and status='2' and outtime>'".(time()-3600*12)."'";
       if(executeSQLQuery($sql)['count']>20){
           if($row['status'] == 1){
               $sql = "update `pay_qrlist` set `stat`='0' ,`status`='0' where `id`='{$row['id']}'";
               executeSQLQuery($sql,'update');
               $sub = $conf['sitename'].' - 二维码风控提醒';
               	$msg = '尊敬的用户您好 您的PID:'.$row['pid'].' 名下二维码经系统检测 您的二维码ID：'.$row['id'].'疑似被'.$gf.'风控，本系统已自动将您的此二维码改变为掉线状态，如确认无异常，可在商户中心修改您的此二维码为正常即可，其次在12小时内请勿打开风控检测，否则将会继续风控！';
                send_mail($userrow['email'], $sub, $msg);
                echo "二维码ID:".$row['id']."经检查疑似风控已改变二维码状态---";
           }
       }
       echo 'Cron Ok ';
    }
    
    public function qlwzf(){
        $limit=100;//每次执行条数
        Examine::cronkey(input("get")['key']);
        $sql = "SELECT * FROM pay_order WHERE  status='2' and outtime<'".(time()-3600*24)."' limit $limit";
        $rs = safeSqlQuery($sql, []);
         $qlsl = ($rs['count'] > $limit) ? $limit : $rs['count'];
         if($rs['count']>0){
             $sql = "DELETE FROM pay_order WHERE status='2' and outtime<'".(time()-3600*24)."' limit $limit ";
             executeSQLQuery($sql,'delete');
             echo "清理成功本轮共计清理：" .$qlsl."条---";
         }
         echo 'Cron Ok ';
    }
    
    public function qqpcgxck(){
        Examine::cronkey(input("get")['key']);
        $currentTime = time();
       $sql=  "SELECT * FROM pay_qrlist WHERE `xf_stat`='1' AND `status`='1' AND `type` ='qqpay' AND `hook_type` = '0' AND UNIX_TIMESTAMP(chatime) < $currentTime LIMIT ".cronlimit; 
       $rs = safeSqlQuery($sql, []);
       if($rs['count']>0){
           foreach ($rs['result'] as $res){
             echo  QQPCGX($res, true);
           }
       }
       echo 'Cron Ok ';
    }
    
    public function sxorder(){
        Examine::cronkey(input("get")['key']);
        $time=time();//当前时间戳
       $sql= "SELECT * from `pay_order` WHERE `status`='0' and `outtime`<'{$time}' order by rand() LIMIT ".cronlimit; 
       $rs = safeSqlQuery($sql, []);
       if($rs['count']>0){
           foreach ($rs['result'] as $res){
               $sql = "update `pay_order` set `status`='2' where `trade_no`='{$res['trade_no']}'";
               executeSQLQuery($sql,'update');
           }
       }
          $sql =  "SELECT * FROM pay_order WHERE `status` ='0' LIMIT 1";
          $rs = safeSqlQuery($sql, []);
          if($rs['count']>0){
              foreach ($rs['result'] as $row){
                  $sql = "SELECT * FROM pay_qrlist WHERE id='{$row['qr_id']}' AND hook_type='0' AND type='alipay' AND cookie='xfqymckxy' LIMIT ".cronlimit; 
                  $rs = safeSqlQuery($sql, []);
                  if($rs['count']>0){
                      foreach ($rs['result'] as $res){
                          xufeiqymoneymck($res, $row);
                      }
                  }
              }
          }
      echo 'Cron Ok ';
    }
    public function Notify(){
        Examine::cronkey(input("get")['key']);
        $sql =  "SELECT * FROM pay_order WHERE status ='0' LIMIT ".cronlimit; 
        $rs = safeSqlQuery($sql, []);
        if($rs['count']>0){
           
            foreach ($rs['result'] as $row){
                $sql = "SELECT * FROM pay_qrlist WHERE id='{$row['qr_id']}'  AND cookie NOT IN ('xfqymckxy','xfqydmfxy') AND hook_type NOT IN ('1','3')";
                $rs = safeSqlQuery($sql, []);
                if($rs['count']>0){
                    foreach ($rs['result'] as $res){
                         xfnotify_url($res, $row);
                     }
                }
            }
        }
        
       echo 'Cron Ok '; 
    }
    public function user_vips(){
      Examine::cronkey(input("get")['key']);
      $sql =   "SELECT * FROM `pay_user` WHERE `user_vips`='1'  LIMIT ".cronlimit; 
      $rs = safeSqlQuery($sql, []);
      $time = time();
      if($rs['count']>0){
          foreach ($rs['result'] as $row){
              if(strtotime($row['alipay_free_vip_time'])<$time || strtotime($row['wxpay_free_vip_time'])<$time || strtotime($row['qqpay_free_vip_time'])<$time){
                  echo uservips($res);
              }
          }
      }
      echo 'Cron Ok ';
    }
    public function appcron(){
        Examine::cronkey(input("get")['key']);
        $time = time();
        $sql = "SELECT * FROM pay_qrlist WHERE status='1' AND `hook_type`= '3'  AND crontime<'{$time}' LIMIT ".cronlimit; 
        $rs = safeSqlQuery($sql, []);
        if($rs['count']>0){
             foreach ($rs['result'] as $res){
                 $sql = "update `pay_qrlist` set `status`=:status WHERE `id`=:id";
                 $params = array(
                        'status'=>0,
                        'id'=>$res['id']
                    );
                if(safeSqlQuery($sql, $params,'update')){
                    echo 'APP通道：'.$res['id'].'掉线更新成功！ ---';
                }else{
                    echo 'APP通道：'.$res['id'].'掉线更新失败！ ---';
                }
             }
        }
         echo 'Cron Ok ';
    }
    public function wx_dytime(){
        Examine::cronkey(input("get")['key']);
         $time = time() + 35;
         $sql = "SELECT * FROM pay_wechat_trumpet WHERE status='1'  AND login_time<'{$time}' LIMIT ".cronlimit; 
         $rs = safeSqlQuery($sql, []);
         $login_time = time()+60;
        if($rs['count']>0){
            foreach ($rs['result'] as $res){
                $sql = "update `pay_wechat_trumpet` set `login_time`=:login_time WHERE `id`=:id";
                $params = array(
                    'login_time'=>$login_time,
                    'id'=>$res['id']
                );
                if(safeSqlQuery($sql, $params,'update')){
                    echo '店员ID：'.$res['id'].'更新成功！ ---';
                }else{
                    echo '店员ID：'.$res['id'].'更新失败！ ---';
                }
            }
        }
        echo 'Cron Ok ';
    }
}

?>